
----------------接收盾华电警相机上传的图片
--上传类型 ftp,正常图片,存入dhdjlist
--违法图片,存入dhwzlist



local tb_hpys = {}
tb_hpys[0] = 2 --"蓝色"
tb_hpys[1] = 1 --"黄色"
tb_hpys[2] = 0 --"白色"
tb_hpys[3] = 3 --"黑色"
tb_hpys[4] = 4 --"其它"


local utils                    = require "gcs.utils"
local cjson                    = require "cjson"

local config_table = ngx.shared.gcs_config   --声明ngx.shared.gcs_config
local ipshine_table = ngx.shared.gcs_ipshine   --声明ngx.shared.gcs_ipshine
local form_timeout = config_table:get("p_form_timeout")
local form_chunk_size = config_table:get("p_form_chunk_size")
local mongo_table_name = "gcxx"
local format = "jpg"
local cjson_group = config_table:get("p_group_table")
local store_connect_timeout = config_table:get("p_store_connect_timeout")
local store_keep_timeout = config_table:get("p_store_keep_timeout")
local store_keep_count = config_table:get("p_store_keep_count")
local remote_ip                = ngx.var.remote_addr              --获得客户端ip
--utils.gcs_log("error","remote_ip:" ..remote_ip)
-----------校验参数
local tb_parameters = {}
tb_parameters.form_timeout = form_timeout
tb_parameters.form_chunk_size = form_chunk_size
tb_parameters.store_connect_timeout = store_connect_timeout
tb_parameters.store_keep_timeout = store_keep_timeout
tb_parameters.store_keep_count = store_keep_count
tb_parameters.cjson_group = cjson_group

local check_parameters = {
  "store_keep_timeout",                 "store_keep_count",                "cjson_group"
}

local res,err,error_no = utils.check_parameter(tb_parameters,check_parameters)
if not res then
  utils.gcs_log("error","second_line upload,check_parameter is err,err:" .. cjson.encode(err))
  local tb_return = {}
  local vssid = 0
  local code = 1001
  local message = err
  return ngx.say(cjson.encode(tb_return))
end

--------------------------加载函数---------------------

local red_method               = require "gcs.list.method"
local mongo_method             = require "gcs.mongodb.method"
local storage                  = require "resty.gcsdfs.storage"              --加载storage �?
local multipart                = require "resty.gcsdfs.multipart"
local up_utils                 = require "gcs.upload.upload_utils"
local up_hex                   = require "gcs.upload.hex"
local db_method                = require "gcs.db.method"
utils.gcs_log("debug","second_line upload,require complete")


---------------------------拼装信息----------------------------
---定义需要插入的字段放入table�?
--local lkbh,time,Hphm,hpys,xssd,xsfx,cd,wfxwdm,hmsztpzb,csys,clpp,sbbh,tpmc
--local gfile,filename,vssid
local db_data = {}
---获取�?
local form = multipart:new(form_timeout,form_chunk_size)
local res,err = form:parse()
if not res then
  ngx.req.discard_body()
  utils.gcs_log("error","second_line upload,form parse is nil,err:"..cjson.encode(err))
  local tb_return = {}
  local vssid = 0
  local code = 1002
  local message = err
  return ngx.say(cjson.encode(tb_return))
end
local form_res,err = form.fields
if not form_res then
  ngx.req.discard_body()
  utils.gcs_log("error","second_line upload,form_res is nil,err:"..cjson.encode(err))
  local tb_return = {}
  local vssid = 0
  local code = 1003
  local message = err
  return ngx.say(cjson.encode(tb_return))
end
---校验form_res中的??�段是否存�?
local res,err = up_utils.check_form_res(form_res)
if not res then
  ngx.req.discard_body()
  utils.gcs_log("error","second_line upload,check_form_res is err,err:"..cjson.encode(err))
  local tb_return = {}
  local vssid = 0
  local code = 1004
  local message = err
  return ngx.say(cjson.encode(tb_return))
end
utils.gcs_log("debug","second_line upload,check form_res is ok")

local db_data = {}

----------取上传来的filename
local filename = form_res.gfile1.filename
filename = string.sub(filename,1,-5)
utils.gcs_log("error",filename)
--解析fname
local rt,err = up_utils.StringSplit(filename,"-")
if not rt then
  local tb_return = {}
  local vssid = 0
  local code = 1005
  local message = err
  return ngx.say(cjson.encode(tb_return))
end
-------------判断第三个字段是否为number类型,如果是,则表示违章
db_data.time = rt[1]
db_data.txxh = rt[2]
local is_num = tonumber(rt[3])
if not is_num then           --正常过车
  db_data.hphm = up_utils.createIconv("gbk","utf-8",rt[3])
  if rt[4] then              --有号牌颜色
    db_data.hpys = up_utils.createIconv("gbk","utf-8",rt[4])      ---将号牌颜色转成数字
    local l = string.find(db_data.hpys,"蓝")
    if not l then
      local h = string.find(db_data.hpys,"黄")
      if not h then
        local b = string.find(db_data.hpys,"白")
        if not b then
          local h = string.find(db_data.hpys,"黑")
          if not h then
            db_data.hpys = 4
          else
            db_data.hpys = 3
          end
        else
          db_data.hpys = 0
        end
      else
        db_data.hpys = 1
      end
    else
      db_data.hpys = 2
    end
  else
    db_data.hpys = 4
  end
  db_data.zpzs = 1
  db_data.zpxh = 1
  db_data.wfxwdm = "00000"
else                          --违章
  if rt[5] then
    db_data.hphm = up_utils.createIconv("gbk","utf-8",rt[5])
    if rt[6] then
      db_data.hpys = up_utils.createIconv("gbk","utf-8",rt[6])       ---将号牌颜色转成数字
      local l = string.find(db_data.hpys,"蓝")
      if not l then
        local h = string.find(db_data.hpys,"黄")
        if not h then
          local b = string.find(db_data.hpys,"白")
          if not b then
            local h = string.find(db_data.hpys,"黑")
            if not h then
              db_data.hpys = 4
            else
              db_data.hpys = 3
            end
          else
            db_data.hpys = 0
          end
        else
          db_data.hpys = 1
        end
      else
        db_data.hpys = 2
      end
    else
      db_data.hpys = 4
    end
else
  db_data.hphm = "无牌"
  db_data.hpys = 4
end
db_data.zpzs = rt[3]
db_data.zpxh = rt[4]
db_data.wfxwdm = "16251"
end

-------------------------------------开始解析图片流
local _16hex = up_hex._to16hex(table.concat(form_res.gfile1.value)) --获取16进制二进制流
--取字符串中包含的最后一个"ffd9"字断以后的16进制字符串
local re_str = _16hex
while 1 do
  local i,j = string.find(re_str,"FFD9")
  if i then
    re_str = string.sub(re_str,j+1,-1)
  else
    if re_str == _16hex then
      re_str = nil
    end
    break
  end
end
local _16hex_kkmc = string.sub(re_str,1,64) --取出16进制字符串中包含的车道信息(16进制)
--local _16hex_hdsj = string.sub(re_str,65,88)   --红灯时间
--local _16hex_pssj = string.sub(re_str,89,112) --拍摄时间
local _16hex_cd   = string.sub(re_str,121,124)  --车道
--------转16进制table
local tb_16hex_kkmc = up_hex._to16tb_hex(_16hex_kkmc)
--local tb_16hex_pssj = up_hex._to16tb_hex(_16hex_pssj)
local tb_16hex_cd = up_hex._to16tb_hex(_16hex_cd)
-------转十进制table
local tb_10hex_kkmc = up_hex._to10tb_hex(tb_16hex_kkmc)
--local tb_10hex_pssj = up_hex._to10tb_hex1(tb_16hex_pssj)
local tb_10hex_cd = up_hex._to10tb_hex(tb_16hex_cd)
local gbk_kkmc = string.char(unpack(tb_10hex_kkmc))
local utf8_kkmc = up_utils.createIconv("gbk","utf-8",gbk_kkmc) or ""
--------------------------------测试输出
db_data.cd = tonumber(table.concat(tb_10hex_cd))
---------通过kkmc以及cd向mysql中查到lkbh,sbbh,xsfx
utils.gcs_log("error",utf8_kkmc)
--[[
local rt1,err = up_utils.StringSplit(utf8_kkmc,"-")                                                   ------取出kkmc
local query = "select * from gcs_ipshine where C_KKMC = "..ngx.quote_sql_str(rt1[1])
query = query .. " and C_CD like '%" .. db_data.cd ..",%;"
local tb_query = db_method.query(query)
utils.gcs_log("error","query: "..query)
if not next(tb_query) then
utils.gcs_log("error","query: "..query)
local tb_return = {}
local vssid = 0
local code = 1111
local message = "mysql select is err"
return ngx.say(cjson.encode(tb_return))
end
utils.gcs_log("error","tb_query: "..cjson.encode(tb_query))
db_data.lkbh = tb_query[1].C_LKBH
db_data.sbbh = tb_query[1].C_SBBH
db_data.xsfx = tb_query[1].C_XSFS
db_data.ip = tb_query[1].C_KEY
--]]
----------------test---------------
db_data.lkbh = "123"
db_data.sbbh = "456"
db_data.xsfx = "786"
db_data.ip = "192.168.1.1"
local rt1,err = up_utils.StringSplit(utf8_kkmc,"-")
local query = "select * from gcs_ipshine where C_KKMC = "..ngx.quote_sql_str(rt1[1])
query = query .. " and C_CD like '%" .. db_data.cd ..",%;"
utils.gcs_log("error","query: "..query)
-----------------------------test---
db_data.xssd = 0
db_data.hpsztpzb = ""
db_data.csys = "未知颜色"
db_data.clpp = "未知品牌"
db_data.tpmc = db_data.lkbh.."-"..db_data.time.."-"..db_data.txxh.."-"..db_data.zpzs.."-"..
  db_data.zpxh.."-"..db_data.hphm.."-"..db_data.hpys.."-"..
  db_data.xssd.."-"..db_data.xsfx.."-"..db_data.cd.."-"..db_data.wfxwdm.."-"..
  db_data.hpsztpzb.."-"..db_data.csys.."-"..db_data.clpp.."-"..db_data.sbbh
db_data.id = db_data.lkbh .."_".. db_data.time.."_" .. db_data.sbbh.."_"..db_data.zpxh
db_data.size = form_res.gfile1.file_size
db_data.md5 = form_res.gfile1.md5
db_data.filename = filename
--存储storage
local st_res = up_utils.get_storage(cjson_group)
local tb_value = {}
local tb_value = {
  timeout = store_connect_timeout,
  keep_timeout = store_keep_timeout,
  keep_count = store_keep_count
}

local gfile,err = up_utils.save_storage(form_res,st_res,tb_value,format)
if not gfile then
  utils.gcs_log("error","second_line upload,save_storage is err,err:"..cjson.encode(err))
  local tb_return = {}
  local vssid = 0
  local code = 1006
  local message = err
  return ngx.say(cjson.encode(tb_return))
end
db_data.gfile = gfile
db_data.str_res = cjson.encode(st_res)


--------------------------插入数据�?--------------------------------
---插入mongodb,记录数据
local n,err = mongo_method.save_data(mongo_table_name,db_data)
if not n then
  utils.gcs_log("error","second_line upload,save mongodb is err,err:"..cjson.encode(err))
  local tb_return = {}
  local vssid = 0
  local code = 1007
  local message = err
  return ngx.say(cjson.encode(tb_return))
end

---插入redis,key,value
local insert_data = {}
insert_data[db_data.id] = cjson.encode(db_data)
local res,err = red_method.set_data(insert_data)
if not res then
  utils.gcs_log("error","second_line upload,save redis is err,err:"..cjson.encode(err))
  local tb_return = {}
  local vssid = 0
  local code = 1015
  local message = err
  return ngx.say(cjson.encode(tb_return))
end


---插入list
if db_data.wfxwdm == "00000" then
  local res,err = red_method.push("l","dhdjlist",db_data.id)
  if not res then
    utils.gcs_log("error","second_line upload,push kklist is err,err:"..cjson.encode(err))
    local tb_return = {}
    local vssid = 0
    local code = 1008
    local msg = err
    return ngx.say(cjson.encode(tb_return))
  end
else
  local res,err = red_method.push("l","dhwzlist",db_data.id)
  if not res then
    utils.gcs_log("error","second_line upload,push kklist is err,err:"..cjson.encode(err))
    local tb_return = {}
    local vssid = 0
    local code = 1008
    local msg = err
    return ngx.say(cjson.encode(tb_return))
  end
end


